package com.heoller.service.generator.postprocess;

import com.heoller.dto.CommonParam;
import com.heoller.dto.CustomParam;
import com.heoller.dto.WriteParam;
import com.google.common.collect.Lists;
import freemarker.template.Template;
import java.util.zip.ZipOutputStream;
import org.springframework.stereotype.Service;

/**
 * @author huliang
 * @date 2021-11-23 15:41
 */
@Service("starterPostProcessService")
public class StarterPostProcessService extends AbstractGeneratorPostProcessService {

    @Override
    public void doPostProcess(ZipOutputStream zip, String module, CustomParam customParam) {
        String javaPath = buildJavaPath(module, customParam);
        WriteParam[] writeParams = new WriteParam[]{
            new WriteParam("/aspect/", "PerfLogAspect.java.ftl", "PerfLogAspect.java"),
            new WriteParam("/config/", "PerfAspectConfiguration.java.ftl", "PerfAspectConfiguration.java"),
            new WriteParam("/config/", "WebMvcAutoConfiguration.java.ftl", "WebMvcAutoConfiguration.java"),
            new WriteParam("/interceptor/", "SecurityInterceptor.java.ftl", "SecurityInterceptor.java"),
            new WriteParam("/run/", "ApplicationRunner.java.ftl", "ApplicationRunner.java"),
        };
        CommonParam commonParam = writeBatch(customParam, module, Lists.newArrayList(writeParams), javaPath, zip);
        String resourcesPath = buildResourcesPath(module, customParam);
        String[] files = {"application.properties", "log4j2.xml", "logback.xml"};
        for (String file : files) {
            Template resourcesTemplate = templateService.getTemplate(module, file + ".ftl");
            String appYmlFile = resourcesPath + "/" + file;
            templateService.writeFile(zip, appYmlFile, resourcesTemplate, commonParam);
        }
    }
}
